//
//  SmallCardContentView.swift
//  小卡位 - 内容
//
//  Created by 千策万划-mini on 2025/5/8.
//

import Foundation

class SmallCardContentView: UIView {
    
    var blockApplyClick:(() -> Void)?
    var blockOverdueRemindClick:((Int) -> Void)?
    
    // MARK: - lazy
    lazy var imageTitle: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(W(123), W(26))
        result.image = UIImage(named: "icon_home_title_logo")
        return result
    }()
    lazy var imageHouse: UIImageView = {
        let result = UIImageView()
        result.isUserInteractionEnabled = true
        result.widthHeight = XY(SCREEN_WIDTH, SCREEN_WIDTH / 375 * 268)
        result.image = UIImage(named: "icon_small_card_top_bg")
        return result
    }()
    lazy var imageLogo: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(W(46), W(46))
        return result
    }()
    lazy var labelTermTitle: UILabel = {
        let result = UILabel()
        result.textColor = UIColor(hex: "#5228D7")
        result.font = UIFont.customSemiBoldFont(size: 14)
        return result
    }()
    lazy var labelProductTitle: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()
    lazy var imageTermBg: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(84, (32))
        result.image = UIImage(named: "icon_small_card_day_bg")
        return result
    }()
    lazy var labelTerm: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()
    lazy var labelAmountTitle: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()
    lazy var labelAmountUnit: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()
    lazy var labelAmount: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()
    lazy var btnApply: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(W(163), W(58))
        result.setImage(UIImage(named: "icon_small_card_apply_now"), for: .normal)
        result.tag = 1
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    
    lazy var imageDayBg: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(W(86), W(86))
        result.image = UIImage(named: "icon_home_day_bg")
        return result
    }()
    lazy var labelRateDay: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()
    lazy var labelDay: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()
    // 逾期提醒
    lazy var autoScView: AutoScView = {
        let result = AutoScView()
        result.isClickValid = true
        result.widthHeight = XY(SCREEN_WIDTH, 0)
        result.pageCurrentColor = COLOR_PRIMARY()
        result.pageDefaultColor = COLOR_LINE
        result.timerStart()
        result.backgroundColor = .clear
        result.blockNum = { [weak self](num: Int) in
            guard let self = self else { return }
            feedback()
            if let block = blockOverdueRemindClick {
                block(num)
            }
        }
        return result
    }()
    
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        widthHeight = XY(SCREEN_WIDTH, 0)
        backgroundColor = .clear
        isUserInteractionEnabled = true

        addSubView()
    }

    @available(*, unavailable)
    required init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 添加
    func addSubView() {
        addSubview(imageTitle)
        addSubview(imageHouse)
        imageHouse.addSubview(imageLogo)
        imageHouse.addSubview(labelProductTitle)
        imageHouse.addSubview(labelTermTitle)
        imageHouse.addSubview(imageTermBg)
        imageTermBg.addSubview(labelTerm)
        
        imageHouse.addSubview(labelAmountTitle)
        imageHouse.addSubview(labelAmountUnit)
        imageHouse.addSubview(labelAmount)
        imageHouse.addSubview(btnApply)
        
        imageHouse.addSubview(imageDayBg)
        imageDayBg.addSubview(labelRateDay)
        imageDayBg.addSubview(labelDay)
        
        addSubview(autoScView)

        resetView()
    }
    
    // MARK: - 点击事件
    @objc func btnClick(_ sender: UIControl) {
        feedback()
        sender.addClickAnimate()
        switch sender.tag {
        case 1: // apply
            if let block = blockApplyClick {
                block()
            }
            break
        default:
            break
        }
    }
    
    // 逾期还款
    @objc func btnRepaymentClick(_ sender: UIControl) {
        feedback()
        sender.addClickAnimate()
        if let block = blockOverdueRemindClick {
            block(sender.tag)
        }
    }
    
    
    func resetView(_ model: ModelHome = ModelHome()) {
        let modelImpulsive: ModelHomeServesImpulsive = model.serves?.impulsive.first ?? ModelHomeServesImpulsive()
        
        imageTitle.centerXTop = XY(SCREEN_WIDTH / 2, W(12))
        imageHouse.centerXTop = XY(SCREEN_WIDTH / 2, imageTitle.bottom+W(20))
        imageLogo.setImage(urlStr: UnPackStr(modelImpulsive.lucky))
        imageLogo.leftTop = XY(W(32), W(13))
        labelProductTitle.resetView(
            title: UnPackStr(modelImpulsive.sooner),
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 22),
            shadowColor: UIColor(hex: "#2D0084"),
            outlineColor: UIColor(hex: "#2D0084")
        )
        labelProductTitle.leftCenterY = XY(imageLogo.right+8, imageLogo.centerY)
        labelTermTitle.fitTitle(UnPackStr(modelImpulsive.waved), variable: 0)
        labelTermTitle.rightTop = XY(SCREEN_WIDTH-imageLogo.left, W(10))
        imageTermBg.centerXTop = XY(labelTermTitle.centerX, labelTermTitle.bottom+W(2))
        labelTerm.resetView(
            title: UnPackStr(modelImpulsive.peeping),
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 14),
            shadowColor: UIColor(hex: "#4105A0"),
            outlineColor: UIColor(hex: "#3B0B9A")
        )
        labelTerm.centerXCenterY = XY(imageTermBg.width/2, imageTermBg.height/2)
        
        labelAmountTitle.resetView(
            title: UnPackStr(modelImpulsive.bold),
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 16),
            shadowColor: UIColor(hex: "#4105A0"),
            outlineColor: UIColor(hex: "#3B0B9A")
        )
        labelAmountTitle.leftTop = XY(imageLogo.left, imageLogo.bottom+W(26))
        
        labelAmountUnit.resetView(
            title: "₱",
            textColor: COLOR_WHITE(),
            font: UIFont.customExtraBoldFont(size: 30),
            shadowColor: UIColor(hex: "#4105A0"),
            outlineColor: UIColor(hex: "#3B0B9A")
        )
        labelAmountUnit.left = imageLogo.left
        
        labelAmount.resetView(
            title: UnPackStr(modelImpulsive.freedom),
            textColor: COLOR_WHITE(),
            font: UIFont.customExtraBoldFont(size: 48),
            shadowColor: UIColor(hex: "#4105A0"),
            outlineColor: UIColor(hex: "#3B0B9A")
        )
        labelAmount.leftTop = XY(labelAmountUnit.right+4, labelAmountTitle.bottom-W(5))
        labelAmountUnit.bottom = labelAmount.bottom-W(10)
        
        btnApply.leftBottom = XY(imageLogo.left, imageHouse.height-W(34))
        
        // 小太阳
        imageDayBg.rightBottom = XY(SCREEN_WIDTH-W(9.8), imageHouse.height-W(66))
        // 利率 - 日
        let rate = UnPackStr(modelImpulsive.nervous)
        let unit = UnPackStr(modelImpulsive.appear).uppercased() // 大写
        
        labelRateDay.removeAllSubviews()
        labelDay.removeAllSubviews()
        labelRateDay.isHidden = true
        labelDay.isHidden = true
        if isStr(rate) {
            labelRateDay.resetView(
                title: rate,
                textColor: COLOR_WHITE(),
                font: UIFont.customExtraBoldFont(size: 18),
                shadowColor: UIColor(hex: "#5F2F0D"),
                outlineColor: UIColor(hex: "#5F2F0D")
            )
            labelDay.resetView(
                title: unit,
                textColor: COLOR_WHITE(),
                font: UIFont.customExtraBoldFont(size: 18),
                shadowColor: UIColor(hex: "#5F2F0D"),
                outlineColor: UIColor(hex: "#5F2F0D")
            )
        }
        let transform = CGAffineTransform(rotationAngle: -CGFloat.pi / 8)
        labelRateDay.centerXCenterY = XY(imageDayBg.width / 2 - W(9), imageDayBg.height / 2 - W(7))
        labelRateDay.transform = transform
        labelDay.centerXTop = XY(labelRateDay.centerX + W(8), labelRateDay.bottom - W(24))
        labelDay.transform = transform
        labelRateDay.isHidden = false
        labelDay.isHidden = false
        
        // 逾期提醒
        autoScView.height = 0.0
        if !model.farmyard.impulsive.isEmpty {
           
            var listView: [UIImageView] = []
            for i in 0..<model.farmyard.impulsive.count {
                let imageBg = UIImageView()
                imageBg.widthHeight = XY(SCREEN_WIDTH, SCREEN_WIDTH/375*88)
                imageBg.isUserInteractionEnabled = true
                imageBg.image = UIImage(named: "icon_small_card_overdue_reminder_bg")
                imageBg.leftTop = XY(SCREEN_WIDTH*CGFloat(i), 0)
                autoScView.height = imageBg.height
                
                let btn = UIButton()
                btn.widthHeight = XY(W(74), W(33))
                btn.setBackgroundImage(UIImage(named: "icon_login_code_btn"), for: .normal)
                btn.rightTop = XY(imageBg.width-W(30), W(18))
                imageBg.addSubview(btn)
                btn.tag = i
                btn.addTarget(self, action: #selector(btnRepaymentClick(_:)), for: .touchUpInside)
                
                let labelBtnTitle = OutlineLabel()
                labelBtnTitle.resetView(
                    title: "Repayment",
                    textColor: COLOR_WHITE(),
                    font: UIFont.customSemiBoldFont(size: 12),
                    shadowColor: UIColor(hex: "#004600"),
                    outlineColor: UIColor(hex: "#1B4506")
                )
                labelBtnTitle.centerXCenterY = XY(btn.width/2, btn.height/2-1)
                btn.addSubview(labelBtnTitle)
                
                let labelTitle = UILabel()
                labelTitle.textColor = UIColor(hex: "#5F2F0D")
                labelTitle.font = UIFont.customSemiBoldFont(size: 14)
                labelTitle.numberOfLines = 0
                labelTitle.lineSpace = 0
                labelTitle.fitTitle(UnPackStr(model.farmyard.impulsive[i].persisted), variable: btn.left-10-W(30))
                labelTitle.leftCenterY = XY(W(30), btn.centerY)
                imageBg.addSubview(labelTitle)
                
                listView.append(imageBg)
            }
            self.autoScView.resetView(imageAry: listView)
            autoScView.pageControlToBottom = W(19)
            autoScView.pageControl.centerX = SCREEN_WIDTH/2
        }
        autoScView.leftTop = XY(0, imageHouse.bottom)
        
        height = autoScView.bottom
    }
}
